// 云函数入口文件
const cloud = require('wx-server-sdk')
const QcloudSms = require("qcloudsms_js")
const appid = 1400232763 // 替换成您申请的云短信 AppID 以及 AppKey
const appkey = "da6a29a1813bb87b82ed0fadb7c0eac3"
const nationcode = 86 

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => new Promise((resolve, reject) => {
  /* 单发短信示例为完整示例，更多功能请直接替换以下代码 */
  var qcloudsms = QcloudSms(appid, appkey);
  var ssender = qcloudsms.SmsSingleSender();
  var templateId = event.templateId
  var smsSign = event.smsSign
  var params = event.params
  var mobile = event.mobile
  ssender.sendWithParam(nationcode, mobile, templateId, params, smsSign, "", "", (err, res, resData) => {
    /* 设置请求回调处理, 这里只是演示，您需要自定义相应处理逻辑 */
    if (err) {
      console.log("err: ", err);
      reject({ err })
    } else {
      resolve({ res: res.req, resData })
    }
  }
  );

})